package com.zfzs.post.modules.agent.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.zfzs.post.modules.agent.domain.AgentInfo;
import com.zfzs.post.modules.agent.repository.AgentInfoRepository;
import com.zfzs.post.modules.agent.service.AgentInfoService;
import com.zfzs.post.modules.agent.service.dto.AgentInfoDTO;
import com.zfzs.post.modules.agent.service.dto.AgentInfoQueryCriteria;
import com.zfzs.post.modules.agent.service.mapper.AgentInfoMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import com.zfzs.post.modules.agent.repository.AgentInfoRepository;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import com.zfzs.post.modules.agent.service.dto.AgentInfoDTO;
import com.zfzs.post.modules.agent.service.dto.AgentInfoQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Optional;

/**
* @author ck
* @date 2019-08-07
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentInfoServiceImpl implements AgentInfoService {

    @Autowired
    private AgentInfoRepository agentInfoRepository;

    @Autowired
    private AgentInfoMapper agentInfoMapper;

    @Override
    public Object queryAll(AgentInfoQueryCriteria criteria, Pageable pageable){
        Page<AgentInfo> page = agentInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(agentInfoMapper::toDto));
    }

    @Override
    public Object queryAll(AgentInfoQueryCriteria criteria){
        return agentInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
    }

    @Override
    public AgentInfo findById(Long id) {
        Optional<AgentInfo> agentInfo = agentInfoRepository.findById(id);
        ValidationUtil.isNull(agentInfo,"AgentInfo","id",id);
        return agentInfo.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgentInfo create(AgentInfo resources) {
        resources.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return agentInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AgentInfo resources) {
        resources.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        Optional<AgentInfo> optionalAgentInfo = agentInfoRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalAgentInfo,"AgentInfo","id",resources.getId());
        AgentInfo agentInfo = optionalAgentInfo.get();
        agentInfo.copy(resources);
        agentInfoRepository.save(agentInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        agentInfoRepository.deleteById(id);
    }

    @Override
    public AgentInfo findByNickName(String nickName) {
        return agentInfoRepository.findByNickName(nickName);
    }
}